package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.SysBuildBuilding;
import com.ruoyi.system.mapper.SysBuildBuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysBuildHouse;
import com.ruoyi.system.mapper.SysBuildUnitMapper;
import com.ruoyi.system.domain.SysBuildUnit;
import com.ruoyi.system.service.ISysBuildUnitService;

/**
 * 单元管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-28
 */
@Service
public class SysBuildUnitServiceImpl implements ISysBuildUnitService
{
    @Autowired
    private SysBuildUnitMapper sysBuildUnitMapper;
    @Autowired
    private SysBuildBuildingMapper sysBuildBuildingMapper;

    /**
     * 查询单元管理
     *
     * @param id 单元管理ID
     * @return 单元管理
     */
    @Override
    public SysBuildUnit selectSysBuildUnitById(Long id)
    {
        return sysBuildUnitMapper.selectSysBuildUnitById(id);
    }

    /**
     * 查询单元管理列表
     *
     * @param sysBuildUnit 单元管理
     * @return 单元管理
     */
    @Override
    public List<SysBuildUnit> selectSysBuildUnitList(SysBuildUnit sysBuildUnit)
    {
        return sysBuildUnitMapper.selectSysBuildUnitList(sysBuildUnit);
    }

    /**
     * 新增单元管理
     *
     * @param sysBuildUnit 单元管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysBuildUnit(SysBuildUnit sysBuildUnit)
    {
        Long buildid = sysBuildUnit.getBuildid();
        if (sysBuildBuildingMapper.selectSysBuildBuildingById(buildid) == null) {
            throw new CustomException("不存在此楼幢，请核对");
        }
        int rows = sysBuildUnitMapper.insertSysBuildUnit(sysBuildUnit);
        insertSysBuildHouse(sysBuildUnit);
        return rows;
    }

    /**
     * 修改单元管理
     *
     * @param sysBuildUnit 单元管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysBuildUnit(SysBuildUnit sysBuildUnit)
    {
        sysBuildUnitMapper.deleteSysBuildHouseByUnitId(sysBuildUnit.getId());
        insertSysBuildHouse(sysBuildUnit);
        return sysBuildUnitMapper.updateSysBuildUnit(sysBuildUnit);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param units 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildUnitTreeSelect(List<SysBuildUnit> units)
    {
        List<SysBuildBuilding> buildTrees = buildUnitTree(units);
        //map(TreeSelect::new)这就是构建一个下拉树的结构，将找到的数据作为列表集合放进去
        return buildTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param units 部门列表
     * @return 树结构列表
     */
    @Override
    public List<SysBuildBuilding> buildUnitTree(List<SysBuildUnit> units)
    {
        List<SysBuildBuilding> returnList = new ArrayList<>();
        Set<Long> buildIds = new HashSet<>();
        for (SysBuildUnit unit : units) {
            buildIds.add(unit.getBuildid());
        }
        for (Long buildId : buildIds) {
            SysBuildBuilding building = sysBuildBuildingMapper.selectSysBuildBuildingById(buildId);
            List<SysBuildUnit> unitChildren = new ArrayList<>();
            building.setSysBuildUnitList(null);
            for (SysBuildUnit unit : units) {
                if (unit.getBuildid().equals(buildId)) {
                    unitChildren.add(unit);
                }
            }
            building.setSysBuildUnitList(unitChildren);
            returnList.add(building);
        }
        return returnList;
    }

    /**
     * 批量删除单元管理
     *
     * @param ids 需要删除的单元管理ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysBuildUnitByIds(Long[] ids)
    {
        sysBuildUnitMapper.deleteSysBuildHouseByUnitIds(ids);
        return sysBuildUnitMapper.deleteSysBuildUnitByIds(ids);
    }

    /**
     * 删除单元管理信息
     *
     * @param id 单元管理ID
     * @return 结果
     */
    @Override
    public int deleteSysBuildUnitById(Long id)
    {
        sysBuildUnitMapper.deleteSysBuildHouseByUnitId(id);
        return sysBuildUnitMapper.deleteSysBuildUnitById(id);
    }

    /**
     * 新增房间管理信息
     *
     * @param sysBuildUnit 单元管理对象
     */
    public void insertSysBuildHouse(SysBuildUnit sysBuildUnit)
    {
        List<SysBuildHouse> sysBuildHouseList = sysBuildUnit.getSysBuildHouseList();
        Long id = sysBuildUnit.getId();
        if (StringUtils.isNotNull(sysBuildHouseList))
        {
            List<SysBuildHouse> list = new ArrayList<SysBuildHouse>();
            for (SysBuildHouse sysBuildHouse : sysBuildHouseList)
            {
                sysBuildHouse.setUnitId(id);
                list.add(sysBuildHouse);
            }
            if (list.size() > 0)
            {
                sysBuildUnitMapper.batchSysBuildHouse(list);
            }
        }
    }
}
